import {deepCopy} from "@/utils";
import {getAddPlanData} from "@/api/shippingPlan";
import {PLAN_TYPE_FL, PLAN_TYPE_MW} from "@/utils/shippingPlan";

let cacheFlWarehouseList = [];
let cacheWarehouseList = [];
let cacheTransferTypeList = [];

export default {
  name: 'addShippingPlanMixin',
  computed: {
    showPlanTypeList(){
      const arr = [];
      this.planTypeList.forEach(item=>{
        if(this.flWarehouseList.length === 0 && item.label === PLAN_TYPE_FL){
          return;
        }
        arr.push({
          label: item.label,
          text: item.text,
        })
      })
      return arr;
    },
  },
  data(){
    return {
      planDataLoading: false,
      warehouseList: [],
      flWarehouseList: [],
      transferTypeList: [],
      planTypeList: [
        {
          label: PLAN_TYPE_FL,
          text: '头程货件',
        },
        {
          label: PLAN_TYPE_MW,
          text: '移仓货件',
        },
      ],
    }
  },
  created() {
    this.setPlanData()
  },
  methods:{
    setPlanData(){
      if(cacheFlWarehouseList.length === 0 || cacheWarehouseList.length  === 0 || cacheTransferTypeList.length  === 0){
        this.getAddPlanData()
      }else{
        this.flWarehouseList = cacheFlWarehouseList
        this.warehouseList = cacheWarehouseList
        this.transferTypeList = cacheTransferTypeList
        this.finishWarehouseApi()
      }
    },
    // 获取相关数据
    getAddPlanData(params = {}){
      this.planDataLoading = true
      getAddPlanData()
        .then(res=>{
          this.planDataLoading = false
          this.flWarehouseList = deepCopy(res.data.flWarehouseList)
          this.warehouseList = deepCopy(res.data.warehouseList)
          this.transferTypeList = deepCopy(res.data.transferType)
          if(cacheFlWarehouseList.length === 0){
            cacheFlWarehouseList = this.flWarehouseList
          }
          if(cacheWarehouseList.length === 0){
            cacheWarehouseList = this.warehouseList
          }
          if(cacheTransferTypeList.length === 0){
            cacheTransferTypeList = this.transferTypeList
          }
          this.finishWarehouseApi()
        })
    },
    // 获取完api后处理
    finishWarehouseApi(){

    },
    getTransferName(transferType){
      const item = this.transferTypeList.find(item=> item.transferType === transferType)
      return item ? item.typeName : ''
    },
  },
}
